package com.yuexunit.finance.project.action.v1;

import java.math.BigDecimal;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.context.FinanceSessionContext;
import com.yuexunit.finance.project.action.v1.dto.FinanceLoanCastor;
import com.yuexunit.finance.project.action.v1.model.BailReturnInfo;
import com.yuexunit.finance.project.entity.BailReturnEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceMoneyStatementEntity;
import com.yuexunit.finance.project.entity.lexicon.AgreeStateEnum;
import com.yuexunit.finance.project.service.BailReturnService;
import com.yuexunit.finance.project.service.FinanceApplicationService;
import com.yuexunit.finance.project.service.FinanceMoneyStatementService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;


/**
 * @author wenwenlong
 */

@Implements(BailReturnActionApi.class)
public class BailReturnActionImpl implements BailReturnActionApi {
	private BailReturnService bailReturnService = ComponentFactory.loadClass(BailReturnService.class);
	private FinanceMoneyStatementService financeMoneyStatementService = ComponentFactory.loadClass(FinanceMoneyStatementService.class);
	private FinanceApplicationService financeApplicationService = ComponentFactory.loadClass(FinanceApplicationService.class);
	
	@Override
	@Transactional
	public void createBailReturn(Long financeId, BigDecimal amount) {
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(financeId);
		if (finance.getBail().subtract(finance.getBailAlreadyReturn()).compareTo(amount) < 0) {
			throw new BusinessException("申请金额超出剩余可退还金额！");
		}
		BailReturnEntity bail = new BailReturnEntity();
		bail.setFinanceId(financeId);
		bail.setFinanceNumber(finance.getFinanceNumber());
		bail.setAmount(amount);
		bailReturnService.createBailReturnInfo(bail);
	}
	
	@Override
	public PageList<BailReturnInfo> inquireBailReturnPageList(String financeNumber, PageCriteria page) {
		PageList<BailReturnEntity> pages = bailReturnService.inquireBailReturnInfoPageList(financeNumber, page);
		PageList<BailReturnInfo> results = new PageList<BailReturnInfo>(page, pages.getTotalRecordCount());
		
		for (BailReturnEntity bail : pages) {
			BailReturnInfo info = new BailReturnInfo();
			info = toBailReturnInfo(bail);
			results.add(info);
		}
		return results;
	}

	@Override
	public BailReturnInfo inquireBailReturnDetail(Long bailReturnInfoId) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		BailReturnInfo info = new BailReturnInfo();
		info = toBailReturnInfo(bail);
		return info;
	}

	private BailReturnInfo toBailReturnInfo (BailReturnEntity bail) {
		BailReturnInfo info = new BailReturnInfo();
		BeanUtils.copyProperties(bail, info);
		info.setAccountingAuditState(bail.getAccountingAuditState() == null ? 0 : bail.getAccountingAuditState().ordinal());
		info.setCeoAuditState(bail.getCeoAuditState() == null ? 0 : bail.getCeoAuditState().ordinal());
		info.setChiefOperationAuditState(bail.getChiefOperationAuditState() == null ? 0 : bail.getChiefOperationAuditState().ordinal());
		info.setFinancialAuditState(bail.getFinancialAuditState() == null ? 0 : bail.getFinancialAuditState().ordinal());
		info.setLeaderAuditState(bail.getLeaderAuditState() == null ? 0 : bail.getLeaderAuditState().ordinal());
		info.setOperationBusinessAuditState(bail.getOperationBusinessAuditState() == null ? 0 : bail.getOperationBusinessAuditState().ordinal());
		return info;
	}

	@Override
	public void auditBailReturnOperationBusiness(Long bailReturnInfoId, String operationBusinessAudit, Integer operationBusinessAuditState) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		bail.setOperationBusinessAudit(operationBusinessAudit);
		bail.setOperationBusinessAuditState(AgreeStateEnum.valueOf(operationBusinessAuditState));
		bail.setOperationBusinessAuditor(FinanceSessionContext.getAccountFullName());
		bail.setOperationBusinessAuditorId(FinanceSessionContext.getAccountId());
		bail.setOperationBusinessAuditDate(DateUtil.nowDate());
		bailReturnService.updateBailReturnInfo(bail);
	}

	@Override
	public void auditBailReturnChiefOperation(Long bailReturnInfoId, String chiefOperationAudit, Integer chiefOperationAuditState) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		bail.setChiefOperationAudit(chiefOperationAudit);
		bail.setChiefOperationAuditState(AgreeStateEnum.valueOf(chiefOperationAuditState));
		bail.setChiefOperationAuditor(FinanceSessionContext.getAccountFullName());
		bail.setChiefOperationAuditorId(FinanceSessionContext.getAccountId());
		bail.setChiefOperationAuditDate(DateUtil.nowDate());
		bailReturnService.updateBailReturnInfo(bail);
	}

	@Override
	public void auditBailReturnFinancial(Long bailReturnInfoId, String financialAudit, Integer financialAuditState) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		bail.setFinancialAudit(financialAudit);
		bail.setFinancialAuditState(AgreeStateEnum.valueOf(financialAuditState));
		bail.setFinancialAuditor(FinanceSessionContext.getAccountFullName());
		bail.setFinancialAuditorId(FinanceSessionContext.getAccountId());
		bail.setFinancialAuditDate(DateUtil.nowDate());
		bailReturnService.updateBailReturnInfo(bail);
	}

	@Override
	public void auditBailReturnLeader(Long bailReturnInfoId, String leaderAudit, Integer leaderAuditState) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		bail.setLeaderAudit(leaderAudit);
		bail.setLeaderAuditState(AgreeStateEnum.valueOf(leaderAuditState));
		bail.setLeaderAuditor(FinanceSessionContext.getAccountFullName());
		bail.setLeaderAuditorId(FinanceSessionContext.getAccountId());
		bail.setLeaderAuditDate(DateUtil.nowDate());
		bailReturnService.updateBailReturnInfo(bail);
	}

	@Override
	@Transactional
	public void auditBailReturnAccounting(Long bailReturnInfoId, String accountingAudit, Integer accountingAuditState) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		bail.setAccountingAudit(accountingAudit);
		bail.setAccountingAuditState(AgreeStateEnum.valueOf(accountingAuditState));
		bail.setAccountingAuditor(FinanceSessionContext.getAccountFullName());
		bail.setAccountingAuditorId(FinanceSessionContext.getAccountId());
		bail.setAccountingAuditDate(DateUtil.nowDate());
		bailReturnService.updateBailReturnInfo(bail);
		
		FinanceApplicationEntity finance = financeApplicationService.inquireFinanceApplicationEntity(bail.getFinanceId());
		FinanceMoneyStatementEntity moneyEntity = FinanceLoanCastor.toFinanceBailReturn(finance, bail);
		financeMoneyStatementService.createFinanceMoneyStatement(moneyEntity);
	}

	@Override
	public void auditBailReturnCeo(Long bailReturnInfoId, String ceoAudit, Integer ceoAuditState) {
		BailReturnEntity bail = bailReturnService.inquireBailReturnInfo(bailReturnInfoId);
		bail.setCeoAudit(ceoAudit);
		bail.setCeoAuditState(AgreeStateEnum.valueOf(ceoAuditState));
		bail.setCeoAuditor(FinanceSessionContext.getAccountFullName());
		bail.setCeoAuditorId(FinanceSessionContext.getAccountId());
		bail.setCeoAuditDate(DateUtil.nowDate());
		bailReturnService.updateBailReturnInfo(bail);
	}
}
